//XML报文解析
importClass(Packages.org.framework2.utils.XMLReader);
importClass(Packages.org.framework2.utils.HttpClientUtil);
importClass(Packages.org.framework2.utils.DBTools);
importClass(Packages.org.framework2.utils.LogUtils);

LogUtils.initLogID();
LogUtils.info('js.mvNotify:','请求接口名称:[MV类接口]');

//获取请求报文参数
var xmlData = request.getParameter('xmldata');
//解析XML数据
var xmlReader = new XMLReader(xmlData);
var transactionID = xmlReader.getNodeText('InnerBus.SvcCont.DeployMVReq.TransactionID');
var enableID = xmlReader.getNodeText('InnerBus.SvcCont.DeployMVReq.EnableID');

//到流媒体transactionID
var transactionIDForStream = xmlReader.getNodeText('InnerBus.SvcCont.StreamDeployNotifyReq.TransactionID');
//到流媒体enableID
var enableIDForStream = xmlReader.getNodeText('InnerBus.SvcCont.StreamDeployNotifyReq.EnableID');


var queryBusLogData;

//MV类到各平台）
if(transactionID != null && enableID != null)
{
	//根据参数查询数据库，取得IBUS记录
	var sql = 'select * from t_wstm_cms_ibus t where t.transid = \'' + transactionID + '\'';
	sql += ' ' + 'and t.message1a like \'%<EnableID>' + enableID  + '</EnableID>%\'';
	sql += ' ' + 'and t.type = \'00\'';
	sql += ' ' + 'and t.use = \'00\' order by t.sequence asc';
	queryBusLogData = dbTools.select(sql);
	LogUtils.info('js.mvNotify:',sql);
}

//MV类到流媒体）
if(transactionIDForStream != null && enableIDForStream != null)
{
	//根据参数查询数据库，取得IBUS记录
	var sql = 'select * from t_wstm_cms_ibus t where t.transid = \'' + transactionIDForStream + '\'';
	sql += ' ' + 'and t.message1a like \'%<EnableID>' + enableIDForStream  + '</EnableID>%\'';
	sql += ' ' + 'and t.type = \'00\'';
	sql += ' ' + 'and t.use = \'00\' order by t.sequence asc';
	queryBusLogData = dbTools.select(sql);
	LogUtils.info('js.mvNotify:',sql);
}
	
if(queryBusLogData != null && queryBusLogData.size() > 0)
{
	var responseIbusLog = queryBusLogData.get(0);
	//响应CMS
	var message2a = responseIbusLog.get('message2a');
	var message2b = responseIbusLog.get('message2b');
	var message2c = responseIbusLog.get('message2c');
	var sequence = responseIbusLog.get('sequence');
	message2b = message2b==null?"":message2b;
	message2c = message2c==null?"":message2c;
	response.write(new java.lang.String(message2a + message2b + message2c).replaceAll('\n',''));
	response.getOutputStream2().flush();
	LogUtils.info('js.mvNotify:', 'rescontent:' + new java.lang.String(message2a + message2b + message2c).replaceAll('\n',''));
	
	//修改数据库，该条数据已使用
	var responseProcid = responseIbusLog.get('procid');
	var responseTransid = responseIbusLog.get('transid');
	var updateResponseIbusLogSql = 'update t_wstm_cms_ibus t set t.use = \'01\' where t.transid =\'' + responseTransid + '\' and  t.procid = \'' + responseProcid + '\'';
	dbTools.update(updateResponseIbusLogSql);
	LogUtils.info('js.mvNotify:',updateResponseIbusLogSql);
	
	//根据参数查询数据库，取得IBUS记录[用responseIbusLog取得的responseTransid避免不同XML取到的值不一样]
	sequence = Number(sequence) + 1;
	var queryRequestCmsBusLogSql = 'select * from t_wstm_cms_ibus t where t.transid = \'' + responseTransid + '\'';
	queryRequestCmsBusLogSql += ' ' + 'and t.type = \'01\'';
	queryRequestCmsBusLogSql += ' ' + 'and t.sequence = \''+ sequence +'\'';
	queryRequestCmsBusLogSql += ' ' + 'and t.use = \'00\'';         
	var requestCmsBusLogData = dbTools.select(queryRequestCmsBusLogSql);
	LogUtils.info('js.mvNotify:',queryRequestCmsBusLogSql);

	if(requestCmsBusLogData !=null && requestCmsBusLogData.size() > 0)
	{
		var requestCmsBusLog = requestCmsBusLogData.get(0);
		var message1a = requestCmsBusLog.get('message1a');
		var message1b = requestCmsBusLog.get('message1b');
		var message1c = requestCmsBusLog.get('message1b');
		message1b = message1b==null?"":message1b;
		message1c = message1c==null?"":message1c;
		//修改数据库，该条数据已使用
		var requestProcid = requestCmsBusLog.get('procid');
		var requestTransid = requestCmsBusLog.get('transid');
		var url = requestCmsBusLog.get('url');
		var updateRequestCmsBusLogSql = 'update t_wstm_cms_ibus t set t.use = \'01\' where t.transid =\'' + requestTransid + '\' and  t.procid = \'' + requestProcid + '\'';
		dbTools.update(updateRequestCmsBusLogSql);
		LogUtils.info('js.mvNotify:',updateRequestCmsBusLogSql);
		
		var map = new java.util.HashMap();
		map.put('xmldata',new java.lang.String(message1a + message1b + message1c).replaceAll('\n',''));
		LogUtils.info('js.mvNotify:',url);
		LogUtils.info('js.mvNotify:',map.get('xmldata'));
		HttpClientUtil.post('http://10.25.102.143:5657' + url, 60000, map);
	}
}
else
{
	//记录失败日志
	var insertLogSql = 'insert into T_WSTM_ERROR_LOG(';
	insertLogSql += 'ID,PROCID,TRANSID,ENABLEID,TONEID,EVENTTYPE,EXCEPTION';
	insertLogSql += ')values(';
	insertLogSql += "SEQ_T_WSTM_ERROR_LOG_ID.NEXTVAL";
	insertLogSql += ",null";
	insertLogSql += ",'"+transactionID+"'";
	insertLogSql += ",'"+enableID+"'";
	insertLogSql += ",null";
	insertLogSql += ",null";
	insertLogSql += ",'没有对应IBUS记录！')";
	dbTools.update(insertLogSql);
	LogUtils.info('js.mvNotify:',insertLogSql);
}


